using System;
using System.Reflection;
using Mbs.ComponentModel.Container.Context;

namespace Mbs.ComponentModel.Container.Injection
{
   
    [Serializable]
    public abstract class InjectAdapter<TMember> : MarshalAndDisposableObject, IInject<TMember> where TMember : MemberInfo
    {
        protected IKernel Kernel { get; private set; }
        public IInjectContext InjectContext { get; private set; }

        void ICommand<IInjectContext>.Execute(IInjectContext ctx)
        {
            Execute(ctx as IInjectContext<TMember>);
        }


        public virtual void Execute(IInjectContext<TMember> ctx)
        {
            if (ctx == null
                || ctx.Kernel == null
                || ctx.Instance == null)
                return;

            Kernel = ctx.Kernel as IKernel;
            InjectContext = ctx;

            DoExecute(ctx);
        }

        protected abstract void DoExecute(IInjectContext<TMember> ctx);

    }
}
